﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Mirkuz.UI
{
    public class TBaseControl : Control
    {
        bool _istransparent = true;
        //float Hscale = 1;
        //float Vscale = 1;
        
        public TBaseControl() : base()
        {
            //if (Screen.PrimaryScreen.Bounds.Width == 480)
            //    Hscale = 2;
            //if (Screen.PrimaryScreen.Bounds.Height == 640)
            //    Vscale = 2;
            //if (Screen.PrimaryScreen.Bounds.Height == 800)
            //    Vscale = 3.333f;
            //if (Screen.PrimaryScreen.Bounds.Height == 400)
            //    Vscale = 1.25f;
        }
        
        public bool IsTransparent
        {
            get { return _istransparent; }
            set { _istransparent = value; OnTransparentChanged(); }
        }

        protected void OnTransparentChanged()
        {
            this.Invalidate();
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            Invalidate();
        }

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            Invalidate();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //Rectangle rsrc = new Rectangle((int)(Left / Hscale), (int)(Top / Vscale), (int)(Width / Hscale),(int) (Height / Vscale));
            //Rectangle rdest = new Rectangle(0, 0, Width, Height);

            Rectangle rsrc = new Rectangle(Left,Top,Width,Height);
            Rectangle rdest = new Rectangle(0, 0, Width, Height); 
            e.Graphics.Clear(Color.Black);
            
            if (Parent == null)
                base.OnPaintBackground(e);
            else
            {
                TForm p = Parent as TForm;

                if ((p.BackgroundImage != null && IsTransparent))
                    e.Graphics.DrawImage(p.BackgroundImage, rdest, rsrc, GraphicsUnit.Pixel);
                else
                    e.Graphics.FillRectangle(new SolidBrush(BackColor), rdest);
            }

            if (this.Site != null && this.Site.DesignMode)
            {
                e.Graphics.DrawRectangle(new Pen(Color.Black), 0, 0, Width - 1, Height - 1);
            }

        }
    }
}
